import { type ExcelColumn, exportSelectedToExcel, exportToExcel } from "@/utils/excel";

export interface UseExcelExportOptions {
  filename: string;
  columns: ExcelColumn[];
}

export const useExcelExport = (options: UseExcelExportOptions) => {
  const { filename, columns } = options;

  const exportData = (data: any[], selectedKeys?: React.Key[], keyField: string = "id") => {
    if (!data || data.length === 0) {
      window.$message?.warning("暂无数据可导出");
      return;
    }

    if (selectedKeys && selectedKeys.length > 0) {
      // 导出选中的数据
      exportSelectedToExcel(
        {
          filename,
          columns,
        },
        data,
        selectedKeys,
        keyField,
      );
      window.$message?.success(`已导出 ${selectedKeys.length} 条选中数据`);
    } else {
      // 导出全部数据
      exportToExcel({
        filename,
        columns,
        data,
      });
      window.$message?.success(`已导出全部 ${data.length} 条数据`);
    }
  };

  return {
    exportData,
  };
};
